//
// Created by moonlight on 25-7-23.
//
#include <iostream>
#include <set>
#include <ctime>
#include <string>
using namespace std;
void printSet(set<int> &s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
void printSet(multiset<int> &s) {
    for (multiset<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
void test_set() {
    set<int> s;
    for (int i = 0; i < 10; i++) {
        s.insert(i);
    }
    s.insert(8);
    printSet(s);
}
void test_constucter() {
    set<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.insert(i);
    }
    printSet(s1);
    set<int> s2(s1);//拷贝
    set<int> s3;
    s3 = s1;

}
void test_bulk() {
    set<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.insert(i);
    }
    if (s1.empty()) {
        cout << "<UNK>" << endl;
    }else {
        cout << s1.size() << endl;
    }
}
void test_swap() {
    set<int> s1;
    set<int> s2;
    for (int i = 0; i < 10; i++) {
        int num = rand() % 20+40;
        s1.insert(i);
        s2.insert(num);
    }
    printSet(s1);
    printSet(s2);
    s1.swap(s2);
    printSet(s1);
    printSet(s2);

}
void test_erase_insert() {
    set<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.insert(i);
    }
    printSet(s1);
    // s1.erase(s1.begin());
    // s1.erase(s1.end());
    s1.erase(6);
    printSet(s1);
    s1.clear();
    printSet(s1);
}
void test_find() {
    set<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.insert(i);
    }
    printSet(s1);
    set<int>::iterator it = s1.find(5);
    if (it != s1.end()) {
        cout << *it << endl;
    }else {
        cout << "not found" << endl;
    }
}
void test_cout() {
    set<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.insert(i);
    }
    cout<<s1.count(5)<<endl;
}
void test_set_multiset() {
    set<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.insert(i);
    }
    multiset<int> s2;
    for (int i = 0; i < 10; i++) {
        s2.insert(i);
    }
    s2.insert(s1.begin(), s1.end());
    printSet(s1);
    printSet(s2);
}
void test_pair() {
    set<int> s1;
    pair<set<int>::iterator, bool> ret = s1.insert(5);
    if (ret.second == true) {
        cout << "insert success" << endl;
    }else {
        cout << "not insert success" << endl;
    }
    pair<set<int>::iterator, bool> ret2 = s1.insert(5);
    if (ret2.second == true) {
        cout << "insert success" << endl;
    }else {
        cout << "not insert success" << endl;
    }

    multiset<int> s2;
    s2.insert(10);
    s2.insert(10);
    multiset<int>::iterator iterator = s2.insert(10);
    for (iterator = s2.begin(); iterator != s2.end(); iterator++) {
        cout << *iterator <<"-multiset-";
    }
}
void test_pair_use() {
    pair<string,int> p("李四",20);
    cout<<"姓名:"<<p.first<<"\t年龄:"<<p.second<<endl;
    pair<string,int> p2 = make_pair("李欣芸",20);
    cout<<"姓名:"<<p2.first<<"\t年龄:"<<p2.second<<endl;
}
class MyCompare {
public:
    bool operator()(int v1,int v2) const {
        return v1>v2;
    }
};
void test_set_sort() {
    set<int,MyCompare> s;//仿函数指定排序规则
    for (int i = 0; i < 10; i++) {
        s.insert(i);
    }
    for (set<int,MyCompare>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it <<" ";
    }
}
//自定义数据类型,排序规则
class Person {
public:
    Person(string name, int age) {
        this->m_name = name;
        this->m_age = age;
    }
    string m_name;
    int m_age;
};
class MyCompare2 {
public:
    bool operator()(const Person &p1,const Person &p2) const {
        return p1.m_age < p2.m_age;
    }
};
void test_sort_person() {
    Person p1("刘备",15);
    Person p2("张飞",25);
    Person p3("关羽",20);
    set<Person,MyCompare2> s;
    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    for (set<Person,MyCompare2>::iterator it = s.begin(); it != s.end(); it++) {
        cout<<it->m_name<<it->m_age<<endl;
    }
}
int main() {
    srand((unsigned int)time(NULL));
    test_sort_person();
    return 0;
}